var Signup = {
	flag: false,
	lan: LANGUAGE.ENGLISH,
	init: function() {
		var user = GetLS("user");
        if(user){
                Push.setTag(user.phone);
                if(user.category == Role.TYPE_FOREIGNER ){
                    lan = LANGUAGE.ENGLISH;
                }
                if(user.category == Role.TYPE_FOREIGNER || user.category == Role.TYPE_CHINESE) {
                    CurrentUser = user;
                    $("#phone, #pwd").blur();
                    var lanPath = lan==0?"ch/":"en/";
                    OpenNewWin(lanPath + "index");
                    PCall("signup", '$("#header").removeClass("hide");');
                                    
                }else{
                    $alert("不支持该类型的用户登录"); 
                    $("#header").removeClass("hide");
                }
                
        }else{
        	$("#header").removeClass("hide");
            Signup.lan = GetLS("language")?GetLS("language"):LANGUAGE.CHINESE;
            $("#language").text((Signup.lan==LANGUAGE.ENGLISH?"中国人":"I am expat"));
            Signup.textInit();
        }
		
	},
	switchLanguage: function(){
		var $lan = $("#language");
		if (Signup.lan == LANGUAGE.CHINESE){
			SetLS("language",LANGUAGE.ENGLISH);
			Signup.lan = LANGUAGE.ENGLISH;
			$lan.text("中国人");
		} else {
			SetLS("language",LANGUAGE.CHINESE);
			Signup.lan = LANGUAGE.CHINESE;
			$lan.text("I am expat");
		}
		Signup.textInit();
	},
	textInit: function(){
		$("#phone").attr("placeholder",CH_EN.phone[Signup.lan]);
		$("#pwd").attr("placeholder",CH_EN.password[Signup.lan]);
		$("#lgnBtn").text(CH_EN.login[Signup.lan]);
		$("#register").text(CH_EN.register[Signup.lan]);
		$("#findpass").text(CH_EN.findpass[Signup.lan]);
	},
	lgn: function() {
		
        if (!Core.check())
             return;
        var phone = $("#phone").val();
        var password = $("#pwd").val();
        
		$ajax({
			url: "/user/lgn",
			data:{phone:phone, password:password},
			success: function(res) {
				var user = res.result[0];
				Push.isAppOne = true;
				Push.setTag(user.phone);
				if(user.category == Role.TYPE_FOREIGNER ){
				    Signup.lan = LANGUAGE.ENGLISH;
				}
				if(user.category == Role.TYPE_FOREIGNER || user.category == Role.TYPE_CHINESE) {
					SetLS("user", res.result[0]);
					CurrentUser = GetLS("user");
					$("#phone, #pwd").blur();
					var lanPath = lan==0?"ch/":"en/";
					SetLS("languagePath",lanPath);
					setTimeout(function(){
                        OpenNewWin(lanPath + "index");
                    },800);
									
				}else
					(CurrentUser.category==30?$alert("不支持该类型的用户登录"):$alertEn("Users do not support this type of user login"));
			}
		});
	},
	lgnDemo: function() {
		$ajax({
			url: "/user/lgn",
			data:{phone:"30", password:"0"},
			success: function(res) {
				if(res.code)
					return $alertEn(res.codemsg);

				var user = res.result[0];
				if(user.category == Role.TYPE_FOREIGNER || user.category == Role.TYPE_CHINESE) {
					SetLS("user", res.result[0]);
					CurrentUser = GetLS("user");
					$("#phone, #password").blur();
					var lanPath = lan==0?"ch/":"en/";
					OpenNewWin(lanPath + "index");				
				}else
					$alert(CurrentUser.category==30?"不支持该类型的用户登录":"Users do not support this type of user login");
			}
		});
	},	
	phoneCode: function() {
		if (Signup.flag)
			return;
		var phone = $("#phone").val();
		if (phone == "") {
			$alertCE("手机号码不能为空<br/>The phone can not be empty");
			return;
		}
		if (phone.length != 11) {
			$alertCE("手机格式错误<br/>Mobile phone format error");
			return;
		}
		Signup.flag = true;
		$("#phoneBtn").text(CurrentUser.category==30?"120秒":"120Seconds");
		var tmo = setInterval(function(){
			var num = parseInt($("#phoneBtn").text().replace(CurrentUser.category==30?"120秒":"120Seconds", ""));
			--num;
			if (num == 0) {
				Signup.flag = false;
				clearInterval(tmo);
				$("#phoneBtn").text(CurrentUser.category==30?"验证码":"identifying code");
			} else {
				$("#phoneBtn").text(num + CurrentUser.category==30?"120秒":"120Seconds");
			}
		}, 1000);
		$ajax({
			url:"/user/verify",
			data:{PHONE:phone}
		});
	},
	guideClose: function(){ 
		//if ($("#never").prop("checked"))
		SetLS("never","never");
		if (GetLS("user"))
  			OpenNewWin('index',5);
  		else
  			OpenNewWin("signup",5);
	},
	jump: function(url){
		var lanPath = lan==0?"ch/":"en/";
		OpenNewWin(lanPath + url);
	},
	sendCode: function() {
        if (!checkMobile($("#phone").val()))
             return;
        if($("#code").text().substring(0,3) == "验证码"){
            SetLS("sendCodeFlag",false);
        }
        var wait=60; 
       // var html1='<div style="line-height:1.5em;color:white;">验证码<br/>Verification Code</div>';
        function time() {
            if (wait == 0) {
                // $("#code").attr("onclick","Signup.sendCode('" + field + "','" + id + "');");  
                $("#code").css("background-color", "#e63232");       
                $("#code").html("验证码<br/>Verification Code");
                $("#code").attr("data-code","0");
                wait = 60;
                SetLS("sendCodeFlag",false);
            } else{
                
                var html=' <div style="width:10em;height:3.5em;margin-left:-1.4em;margin-top:-0.3em;">'+
                    '<div style="width:5em;position:absolute;margin-left:0.2em;">'+
                        '<div style="margin-top:0.1em;">Waiting</div>'+
                        '<div style="margin-top:0.1em;">等待</div>'+
                    '</div>'+
                    '<div style="font-size:2em;color:red;margin-left:2.4em;margin-top:0.2em;position:absolute;text-align:center;" id="sec"></div>'+
                    '<div style="margin-left:7.5em;">'+
                        '<div style="margin-top:0.3em;">Secs</div>'+
                        '<div style="margin-top:0.2em;">秒</div>'+
                    '</div>'+
                '</div>';
                $("#code").html(html);
                $("#code").attr("data-code","1");
                $("#code").css({"background-color":"#97999a","line-height":"3em;"});
                $("#sec").html(wait);
                if(wait<=9){
                    $("#sec").css("margin-left","2.7em");
                }
                wait--;
                SetLS("sendCodeFlag",true);
                setTimeout(function() {
                    time();
                },
                1000)
            }
        }
        if(!GetLS("sendCodeFlag")){
            var ph = $("#phone").val();
            $ajax({
            url: "/user/yanzhengma",
            data:{
                phone: ph
            },
                success: function(res){
                    if(res.result.type == "ok"){
                        time();
                        SetLS("yanzhengma",res.result);
                    }else if(res.result.type == "error"){
                        $alert("验证码获取错误,请检查手机号是否错误!");
                    }
                }
            });
            
        }
    },
    findPWD: function() {
        if (!checkMobile($("#phone").val()))
             return;
        if($("#code").text() == "验证码"){
            SetLS("sendCodeFlag1",false);
        }
        var wait=60; 
        var html=' <div style="width:10em;height:3.5em;margin-left:-1.4em;margin-top:-0.6em;">'+
                    '<div style="width:5em;position:absolute;margin-left:0.2em;">'+
                        '<div style="margin-top:0.1em;">Waiting</div>'+
                        '<div style="margin-top:0.2em;">等待</div>'+
                    '</div>'+
                    '<div style="font-size:2.5em;color:red;margin-left:1.85em;margin-top:0.35em;position:absolute;text-align:center;" id="sec"></div>'+
                    '<div style="margin-left:7.5em;">'+
                        '<div style="margin-top:0.5em;">Secs</div>'+
                        '<div style="margin-top:0.2em;">秒</div>'+
                    '</div>'+
                '</div>';
        $("#code").html(html);
        var html1='<div style="line-height:1.5em;color:white;">验证码<br/>Verification Code</div>';
      
        function time() {
            if (wait == 0) {
                // $("#code").attr("onclick","Signup.sendCode('" + field + "','" + id + "');");  
                $("#code").css("background-color", "#e63232");       
                $("#code").html(html1);
                wait = 60;
                SetLS("sendCodeFlag1",false);
            }else {
                $("#code").css({"background-color":"#97999a","line-height":"3em;"});
                $("#sec").html(wait);
                if(wait<=9){
                    $("#sec").css("margin-left","2.1em");
                }
                wait--;
                SetLS("sendCodeFlag1",true);
                setTimeout(function() {
                    time();
                },
                1000)
            }
        }
        if(!GetLS("sendCodeFlag1")){
            var ph = $("#phone").val();
            $ajax({
                url: "/user/findpwmsg",
                data:{
                    phone: ph
                },
                    success: function(res){
                        if(res.result.type == "ok"){
                            time();
                            SetLS("yanzhengma1",res.result);
                        }else if(res.result.type == "error"){
                            $alert("验证码获取错误,请检查手机号是否错误!");
                        }
                    }
            });
            
        }
    }
}